package com.s59.ch01.controller;


import com.s59.ch01.entity.Department;
import com.s59.ch01.entity.Employee;
import com.s59.ch01.entity.Page;
import com.s59.ch01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserContorller {
    @Autowired
    private UserService userService;

    @RequestMapping("/xlklist")
    @ResponseBody
    public List<Department> xlklist(){
        return userService.listxlk();
    }

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "") String account, @RequestParam(defaultValue = "") String realname, @RequestParam(defaultValue = "0")Integer bid,
                       @RequestParam(defaultValue = "1")Integer pageNo, Model model){
        int pageSize = 3;
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setPageCount(userService.count(account,realname,bid));
        page.setList(userService.listall(account,realname,bid,(pageNo-1)*pageSize,pageSize));
        model.addAttribute("page",page);
        model.addAttribute("xlid",bid);
        model.addAttribute("account",account);
        model.addAttribute("realname",realname);
        return "input";
    }

    @RequestMapping("select")
    @ResponseBody
    public boolean select(String account){
        Employee employee = userService.select(account);
        if(employee!=null){
            return true;
        }
        return false;
    }

    @RequestMapping("add")
    public String add(@ModelAttribute("command")Employee employee, Model model){
        employee.setId(0);
        model.addAttribute("employee",employee);
        return "add";
    }

    @RequestMapping("adds")
    public String adds(@Valid @ModelAttribute("command") Employee employee, BindingResult bindingResult, String[] list, MultipartFile file, Model model){
        employee.setList(Arrays.asList(list));
        if(bindingResult.hasErrors()){
            return "add";
        }
        if(file.isEmpty()){
            model.addAttribute("error","文件必须上传");
            return "add";
        }else{
            List<String> list1 = Arrays.asList(".png",".jpg",".gif",".jpeg");
            String ctx = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if(file.getSize()>1000000){
                model.addAttribute("error","文件大小不能超过1mb");
                return "add";
            }else if(!list1.contains(ctx)){
                model.addAttribute("error","文件格式必须为.png.jpg.gif.jpeg");
                return "add";
            }else {
                String filename = UUID.randomUUID().toString()+ctx;
                employee.setImg(filename);
                try {
                    file.transferTo(new File("E:/img",filename));
                } catch (IOException e) {
                    e.printStackTrace();
                    model.addAttribute("error","上传失败");
                    return "add";
                }
            }
        }
        userService.insertAll(employee);
        int id = userService.selectAccount(employee.getAccount());
        for (String re:employee.getList()){
            userService.insertSkill(re,id);
        }
        return "redirect:/user/list";
    }

    @RequestMapping("update")
    public String update(Integer id, Model model, HttpSession session){
        Employee employee = userService.select1(id);
        List<String > jn = userService.listbmname(id);
        employee.setList(jn);
        model.addAttribute("command",employee);
        session.setAttribute("tx",employee.getImg());
        session.setAttribute("id",employee.getId());
        return "update";
    }

    @RequestMapping("update1")
    public String update1(@Valid @ModelAttribute("command") Employee employee, BindingResult bindingResult, MultipartFile files, String file, String[] list, Model model, HttpSession session){
        employee.setList(Arrays.asList(list));
        employee.setId((Integer) session.getAttribute("id"));
        if(bindingResult.hasErrors()){
            return "update";
        }
        if(files.isEmpty()){
            employee.setImg(file);
        }else {
            List<String> list1 = Arrays.asList(".png",".jpg",".gif",".jpeg");
            String ctx = files.getOriginalFilename().substring(files.getOriginalFilename().lastIndexOf("."));
            if(files.getSize()>1000000){
                model.addAttribute("error","文件太大");
                return "update";
            }else if(!list1.contains(ctx)){
                model.addAttribute("error","文件类型不对");
                return "update";
            }else {
                String filename = UUID.randomUUID().toString()+ctx;
                employee.setImg(filename);
                File f1 = new File("E:/img",file);
                f1.delete();
                try {
                    files.transferTo(new File("e:/img",filename));
                } catch (IOException e) {
                    e.printStackTrace();
                    model.addAttribute("error","文件上传失败");
                    return "update";
                }
            }
        }
        userService.update(employee);
        userService.deleteskill(employee.getId());
        for(String re:list){
            userService.insertSkill(re,employee.getId());
        }
        return "redirect:/user/list";
    }
    @GetMapping("/shangchuan")
    public String shangchuan(Integer id, Model model, HttpSession session){
        Employee employee = userService.select1(id);
        model.addAttribute("command",employee);
        model.addAttribute("id",employee.getId());
        model.addAttribute("img1",employee.getIdcardzm());
        model.addAttribute("img2",employee.getIdcardbm());
        return "shangchuan";
    }

    @PostMapping("/shangchuan")
    public String shangchuan1(MultipartFile[] files, @ModelAttribute("command") Employee employee, BindingResult bindingResult, String img1, String img2, Integer id, Model model){
        List<String> list = Arrays.asList(".png",".jpg");
        employee.setId(id);
        for(int  i = 0;i<files.length;i++){
            String tr = "正面";
            String error = "error1";
            if(i==1){
                tr = "反面";
                error = "error2";
            }
            MultipartFile file = files[i];
            String ctx = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if(file.isEmpty()){
                model.addAttribute(error,"请上传"+tr);
                return "shangchuan";
            }else {
                if(file.getSize()>1000000){
                    model.addAttribute(error,tr+"大小超过1mb");
                    return "shangchuan";
                }else if(!list.contains(ctx)){
                    model.addAttribute(error,tr+"文件类型不对");
                    return "shangchuan";
                }else {
                    String filename = UUID.randomUUID().toString()+ctx;
                    if(i==0){
                        employee.setIdcardzm(filename);
                    }
                    if(i==1){
                        employee.setIdcardbm(filename);
                    }
                    try {
                        file.transferTo(new File("E:/img",filename));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        userService.updateimg(employee);
        File f1 = new File("E:/img",img1);
        f1.delete();
        File f2 = new File("E:/img",img2);
        f2.delete();
        return "redirect:/user/list";
    }
    @RequestMapping("delete")
    public String delete(Integer id, Model model){
        Employee employee = userService.select1(id);
        File file = new File("E:/img",employee.getImg());
        file.delete();
        File file1 = new File("E:/img",employee.getIdcardzm());
        file1.delete();
        File file2 = new File("E:/img",employee.getIdcardbm());
        file2.delete();
        userService.deleteemployee(id);
        userService.deleteskill(id);
        return "redirect:/user/list";
    }
}
